# 加载环境变量
ifneq (,$(wildcard ./.env))
    include .env
    export
endif

# 设置默认值
PORT ?= 8000
EXTERNAL_PORT ?= 8000
API_PREFIX ?= /api/v1
IMAGE_NAME ?= hub-modules-2-modelhubapi2
VERSION ?= latest
NAMESPACE ?= default
DOCKER_REGISTRY ?= 

# 完整镜像名称（带标签）
FULL_IMAGE_NAME = $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/,)$(IMAGE_NAME):$(VERSION)

# 检测操作系统类型
ifeq ($(OS),Windows_NT)
    SLEEP = timeout $(1) > nul 2>&1 || ping -n $$(expr $(1) + 1) 127.0.0.1 > nul 2>&1
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        SLEEP = sleep $(1)
    endif
    ifeq ($(UNAME_S),Darwin)
        SLEEP = sleep $(1)
    endif
endif

# 调试信息
.PHONY: debug
debug:
	@echo "Environment variables:"
	@echo "  IMAGE_NAME: $(IMAGE_NAME)"
	@echo "  VERSION: $(VERSION)"
	@echo "  NAMESPACE: $(NAMESPACE)"
	@echo "  DOCKER_REGISTRY: $(DOCKER_REGISTRY)"
	@echo "  PORT: $(PORT)"
	@echo "  EXTERNAL_PORT: $(EXTERNAL_PORT)"
	@echo "  API_PREFIX: $(API_PREFIX)"
	@echo "  FULL_IMAGE_NAME: $(FULL_IMAGE_NAME)"

# 基本镜像构建命令 - 使用 docker compose
.PHONY: build-compose
build-compose:
	@echo "=== Building Docker image using docker-compose ==="
	@docker compose build
	@echo "Image built: $(IMAGE_NAME):$(VERSION)"
	@docker images | grep $(IMAGE_NAME)

# 使用 Dockerfile 直接构建镜像
.PHONY: build
build:
	@echo "=== Building Docker image from Dockerfile ==="
	@docker build -t $(FULL_IMAGE_NAME) -f Dockerfile .
	@echo "Image built: $(FULL_IMAGE_NAME)"
	@docker images | grep $(IMAGE_NAME)

# 运行容器
.PHONY: run
run:
	@echo "=== Running container from image $(FULL_IMAGE_NAME) ==="
	@echo "Configuration from .env file:"
	@echo "  Internal port: $(PORT)"
	@echo "  External port: $(EXTERNAL_PORT)"
	@echo "  API prefix: $(API_PREFIX)"
	@echo "Stopping and removing existing container if it exists..."
	@docker stop $(IMAGE_NAME)-container 2>/dev/null || true
	@docker rm $(IMAGE_NAME)-container 2>/dev/null || true
	@echo "Starting container with internal port: $(PORT), external port: $(EXTERNAL_PORT)"
	@docker run -d --name $(IMAGE_NAME)-container \
		-p $(EXTERNAL_PORT):$(PORT) \
		-e PORT=$(PORT) \
		-e EXTERNAL_PORT=$(EXTERNAL_PORT) \
		-e API_PREFIX=$(API_PREFIX) \
		-e IMAGE_NAME=$(IMAGE_NAME) \
		-e VERSION=$(VERSION) \
		-e NAMESPACE=$(NAMESPACE) \
		-e DOCKER_REGISTRY=$(DOCKER_REGISTRY) \
		$(FULL_IMAGE_NAME)
	@echo "Container started: $(IMAGE_NAME)-container (internal port: $(PORT), external port: $(EXTERNAL_PORT))"
	@docker ps | grep $(IMAGE_NAME)

# 停止并删除容器
.PHONY: stop
stop:
	@echo "=== Stopping and removing container $(IMAGE_NAME)-container ==="
	@docker stop $(IMAGE_NAME)-container || true
	@docker rm $(IMAGE_NAME)-container || true
	@echo "Container stopped and removed (if existed)"

# 构建并运行
.PHONY: build-run
build-run: build run
	@echo "=== Image built and container started ==="

# 推送镜像到仓库
.PHONY: push
push: build
	@echo "=== Pushing image to registry ==="
	@docker push $(FULL_IMAGE_NAME)
	@echo "Image pushed: $(FULL_IMAGE_NAME)"

# 构建并标记多个版本
.PHONY: build-tags
build-tags:
	@echo "=== Building Docker image with multiple tags ==="
	@docker build -t $(IMAGE_NAME):$(VERSION) -t $(IMAGE_NAME):latest -f Dockerfile .
	@echo "Images built with tags: $(VERSION) and latest"
	@docker images | grep $(IMAGE_NAME)

# 清理镜像
.PHONY: clean
clean:
	@echo "=== Removing Docker image ==="
	@docker rmi $(FULL_IMAGE_NAME) || true
	@echo "Image removed (if existed): $(FULL_IMAGE_NAME)"

# 编辑 .env 文件
.PHONY: edit-env
edit-env:
	@echo "=== Editing .env file ==="
	@if [ "$(OS)" = "Windows_NT" ]; then \
		notepad .env; \
	else \
		vi .env; \
	fi
	@echo ".env file updated"

# 帮助信息
.PHONY: help
help:
	@echo "Available targets:"
	@echo "  debug         - Show current configuration"
	@echo "  build         - Build image from Dockerfile with specified tag"
	@echo "  build-compose - Build image using docker-compose"
	@echo "  build-tags    - Build image with multiple tags (VERSION and latest)"
	@echo "  push          - Build and push image to registry"
	@echo "  run           - Run container from the built image"
	@echo "  stop          - Stop and remove the running container"
	@echo "  build-run     - Build image and run container"
	@echo "  clean         - Remove the built image"
	@echo "  edit-env      - Edit .env file"
	@echo ""
	@echo "Configuration is loaded from .env file. Current values:"
	@echo "  IMAGE_NAME      - Name of the image ($(IMAGE_NAME))"
	@echo "  VERSION         - Image version tag ($(VERSION))"
	@echo "  DOCKER_REGISTRY - Registry prefix ($(DOCKER_REGISTRY))"
	@echo "  NAMESPACE       - Kubernetes namespace ($(NAMESPACE))"
	@echo "  PORT            - Internal port ($(PORT))"
	@echo "  EXTERNAL_PORT   - External port ($(EXTERNAL_PORT))"
	@echo "  API_PREFIX      - API prefix ($(API_PREFIX))"
	@echo ""
	@echo "Example usage:"
	@echo "  # Edit .env file to change configuration"
	@echo "  make edit-env"
	@echo ""
	@echo "  # Show current configuration"
	@echo "  make debug"
	@echo ""
	@echo "  # Build and run with current configuration"
	@echo "  make build-run"

